package ac.il.technion.c236369.hw4.core;

import java.io.IOException;
import java.net.Socket;

import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.log4j.Logger;

/**
 * @author Artem Barger, Maxim Aizenshtein.
 * 
 * Class represent server request entity. Class
 * wraps within connection which related with incoming
 * request we are to handle and http context reference.
 *
 */
public class ServerRequest {

	private final DefaultHttpServerConnection connection =  new DefaultHttpServerConnection( );
	private final HttpContext context = new BasicHttpContext( );
	private final Logger log = Logger.getLogger( ServerRequest.class);
	
	public ServerRequest( final Socket socket) {
		final BasicHttpParams params = new BasicHttpParams( );
		params
			.setIntParameter( CoreConnectionPNames.SO_TIMEOUT, 5000)
			.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
			.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
		try {
			if ( socket != null)
				connection.bind(socket, params);
		} catch (IOException e) {
			// TODO: Need to log the exception here and re-throw it.
			log.error(e);
		}
	}

	/**
	 * @return
	 */
	public DefaultHttpServerConnection getConnection() {
		return connection;
	}

	/**
	 * @return
	 */
	public HttpContext getContext() {
		return context;
	}

}
